package com.woniuxy.education.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.education.entity.Class;
import com.woniuxy.education.entity.PageResult;
import com.woniuxy.education.entity.User;
import com.woniuxy.education.entity.dto.FormDatas;
import com.woniuxy.education.entity.dto.TUInfo;
import com.woniuxy.education.entity.dto.Tclass;
import com.woniuxy.education.mapper.ClassMapper;
import com.woniuxy.education.mapper.TeacherMapper;
import com.woniuxy.education.mapper.UserMapper;
import com.woniuxy.education.service.GradedeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author author
 * @create 2021/4/6
 */

@Service
public class GradedeanServiceImpl implements GradedeanService {

    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    ClassMapper classMapper;
    @Autowired
    UserMapper userMapper;

    // 封装分页信息
    public <T> PageResult<T> getResult(int page, int size, Page<T> pageHelper, List<T> t) {
        // 获取分页信息
        PageResult<T> pageResult = new PageResult<>();
        // 当前页
        pageResult.setCurrentPage(page);
        // 每一页大小
        pageResult.setSize(size);
        // 总条数
        pageResult.setTotal(pageHelper.getTotal());
        // 总页数
        pageResult.setTotalPage(pageHelper.getPages());
        // 查询的数据
        pageResult.setDatas(t);

        return pageResult;
    }

    //查询所有教师的带班信息
    @Override
    public PageResult<Tclass> findTCInfoByPage(int page, int size) {

        // 分页
        Page<Tclass> pageHelper = PageHelper.startPage(page, size);
        List<Tclass> tcInfo = teacherMapper.selectTCInfo(page, size);

        return getResult(page,size,pageHelper,tcInfo);
    }

    //查询所有班级信息
    @Override
    public PageResult<Tclass> findClassInfoByPage(int page, int size) {
        // 分页
        Page<Tclass> pageHelper = PageHelper.startPage(page, size);
        List<Tclass> tcInfo = classMapper.selectClassInfo();

        return getResult(page,size,pageHelper,tcInfo);
    }
    //查询所有没有分配班主任的班级
    @Override
    public PageResult<Class> findNoHeadTeacherByPage(int page, int size) {
        // 分页
        Page<Class> pageHelper = PageHelper.startPage(page, size);
        List<Class> noHeadTeacher = classMapper.selectNoHeadTeacher();

        return getResult(page,size,pageHelper,noHeadTeacher);
    }

    @Override
    public PageResult<Class> findNoDivByPage(int page, int size) {
        // 分页
        Page<Class> pageHelper = PageHelper.startPage(page, size);
        List<Class> noDivClass = classMapper.noDivClass(page, size);

        return getResult(page,size,pageHelper,noDivClass);
    }

    @Override
    public PageResult<TUInfo> findTUInfoByPage(int page, int size) {
        // 分页
        Page<TUInfo> pageHelper = PageHelper.startPage(page, size);
        List<TUInfo> tuInfos = teacherMapper.selectTUInfo(page, size);

        return getResult(page,size,pageHelper,tuInfos);
    }

    @Override
    public boolean addAccountInfo(FormDatas formDatas) {

        if (userMapper.addUser(formDatas.getAccount(),formDatas.getPassword(),formDatas.getName())){
            User user = new User();
            int u_id = user.getId();
            return teacherMapper.addTeacher(u_id,formDatas.getNumber(),formDatas.getName(),formDatas.getSubject(),formDatas.getTel(),formDatas.getPosition());
        }else {
            return false;
        }

    }
    //修改教师账号密码
    @Override
    public boolean changePsw(Integer u_id,String newPassword) {
        return teacherMapper.changePsw(u_id,newPassword);
    }


    //所有教师信息
    @Override
    public List<String> findTeacher(int id) {

        List<Integer> cid = classMapper.findTeacher(id);
        System.out.println(cid);
        List<String> teacherName = new ArrayList<>();
        for (int i = 0;i<cid.size();i++){
            String name = teacherMapper.findTeacher(cid.get(i));
            if ( name != null){
                teacherName.add(name);
            }

        }
        System.out.println(teacherName);
        return teacherName;
    }
    //创建班级
    @Override
    public boolean createClass(Class cls) {

        Class clss = classMapper.selectClassName(cls.getCname());
        System.out.println(cls.getCname());
        System.out.println(clss);
        if ( clss != null){
            return false;
        }else {
            return classMapper.createClass(cls.getCname(),cls.getCreateTime(),cls.getDiv_class());
        }
    }
    //查询班级授课教师信息
    @Override
    public PageResult<Tclass> teacherInfo(int page, int size, String cname) {

        // 分页
        Page<Tclass> pageHelper = PageHelper.startPage(page, size);
        List<Tclass> tcl = teacherMapper.findClassInfo("%"+cname+"%");

        return getResult(page,size,pageHelper,tcl);
    }

    @Override
    public boolean addHead(String name) {
      return teacherMapper.addHead(name);
    }

    @Override
    public PageResult<Tclass> findTCinfoByIf(int page, int size,String cname, String name) {

        // 分页
        Page<Tclass> pageHelper = PageHelper.startPage(page, size);
        List<Tclass> ifInfo = teacherMapper.selectByIf("%"+cname+"%",name);

        return getResult(page,size,pageHelper,ifInfo);
    }


}
